package lab3.nio;

import config.ATMData;
import config.Config;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;

/**
 * Created by Student on 17.12.2014.
 */
public class ClientThroughPutNIO {

    public static final double TESTING_TIME = 30;

    /**
     * When client connected to server,
     * Server send objects to client.
     * Client calculate number of getting objects per second
     *
     * @param args
     */
    public static void main(String[] args) {
        try (SocketChannel sci = SocketChannel.open()) {
            sci.connect(new InetSocketAddress(Config.ADDRESS, Config.PORT));

            try (BufferedInputStream bis = new BufferedInputStream(Channels.newInputStream(sci));
                 ObjectInputStream ois = new ObjectInputStream(bis)) {

                long start = System.currentTimeMillis();
                long end = System.currentTimeMillis();
                int counter = 0;
                while ((end - start) < 1000 * TESTING_TIME) {
                    try {
                        ATMData atmData = (ATMData) ois.readObject();
                        System.out.println("get: " + atmData);
                        counter++;
                    } catch (Exception e) {
                    }
                    end = System.currentTimeMillis();
                }
                System.out.println(counter / TESTING_TIME);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
